package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;

import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceTemplateDao;
import com.jiantong.work.model.yangzhibaoxian.InsuranceTemplate;
import com.jiantong.work.service.yangzhibaoxian.InsuranceTemplateService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 保险方案模板
 */
@Slf4j
@Service
@RpcProvider
public class InsuranceTemplateServiceImpl implements InsuranceTemplateService {
    private InsuranceTemplateDao insuranceTemplateDao;

    @Autowired
    public InsuranceTemplateServiceImpl(InsuranceTemplateDao insuranceTemplateDao) {
        this.insuranceTemplateDao = insuranceTemplateDao;
    }

    @Override
    public Response<InsuranceTemplate> findById(Long id) {
        try {
            InsuranceTemplate insuranceTemplate = (InsuranceTemplate) insuranceTemplateDao.findById(id);
            if (insuranceTemplate == null) {
                log.error("insuranceTemplate(id={}) is not found", id);
                return Response.fail("insuranceTemplate.not.found");
            }

            return Response.ok(insuranceTemplate);
        } catch (Exception e) {
            log.error("failed to find insuranceTemplate id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceTemplate.find.fail");
        }
    }

    @Override
    public Response<List<InsuranceTemplate>> findByIds(List ids) {
        try {
            List<InsuranceTemplate> insuranceTemplateList =  insuranceTemplateDao.findByIds(ids);
            if (insuranceTemplateList == null) {
                log.error("insuranceTemplateList(ids={}) is not found", ids);
                return Response.fail("insuranceTemplateList.not.found");
            }

            return Response.ok(insuranceTemplateList);
        } catch (Exception e) {
            log.error("failed to find insuranceTemplateList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceTemplateList.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceTemplate>> list(Map map) {
        try {
            List<InsuranceTemplate> insuranceTemplateList = insuranceTemplateDao.list(map);
            if (insuranceTemplateList == null) {
                log.error("insuranceTemplateList(ids={}) is not found", map);
                return Response.fail("insuranceTemplateList.not.found");
            }

            return Response.ok(insuranceTemplateList);
        } catch (Exception e) {
            log.error("failed to find insuranceTemplateList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceTemplateList.find.fail");
        }
    }

    @Override
    public Response<Paging<InsuranceTemplate>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<InsuranceTemplate> insuranceTemplatePaging =  insuranceTemplateDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (insuranceTemplatePaging == null) {
                log.error("insuranceTemplatePaging(ids={}) is not found", map);
                return Response.fail("insuranceTemplatePaging not.found");
            }

            return Response.ok(insuranceTemplatePaging);
        } catch (Exception e) {
            log.error("failed to find insuranceRetailPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceRetailPaging.find.fail");
        }
    }

    @Override
    public Boolean update(InsuranceTemplate insuranceTemplate) {
        try {
            Boolean b =  insuranceTemplateDao.update(insuranceTemplate);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(InsuranceTemplate insuranceTemplate) {
        try {
            insuranceTemplateDao.create( insuranceTemplate);
            return  insuranceTemplate.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<InsuranceTemplate> insuranceTemplateList) {
        try {

            return insuranceTemplateDao.creates(insuranceTemplateList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }


}
